package win.start;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * 基于官方example：
 *
 * Created by 张少昆 on 2017/6/11.
 */
public class Examples {
	//demo for the execution of HTTP GET and POST requests using the HttpClient native API
	@Test
	public void r() throws IOException{
		//1, 创建HttpClient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//2, 创建一个请求
		HttpGet httpGet = new HttpGet("http://targethost/homepage");
		//3, 由HttpClient对象发起请求
		CloseableHttpResponse response = httpClient.execute(httpGet);
		// 底层的HTTP连接仍然由response object持有，允许response content直接来自网络socket流。
		// 为了确保正确的释放系统资源，用户必须在一个finally语句中调用CloseableHttpResponse#close()。
		// 注意，如果response content没有完全被消耗的话，底层的连接不能安全地被重用，该链接会被连接管理器关闭然后抛弃。
		try{
			//输出状态码
			System.out.println("response1.getStatusLine(): " + response.getStatusLine());
			//获取response content -- 就是响应体吧？
			HttpEntity httpEntity = response.getEntity();
			// 确保response body被完全消耗掉
			EntityUtils.consume(httpEntity);
		} finally{
			response.close();
		}

		HttpPost httpPost = new HttpPost("http://targethost/login");
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("username", "vip"));
		nvps.add(new BasicNameValuePair("password", "secret"));
		httpPost.setEntity(new UrlEncodedFormEntity(nvps));
		CloseableHttpResponse response2 = httpClient.execute(httpPost);

		try{
			System.out.println(response2.getStatusLine());
			HttpEntity entity2 = response2.getEntity();
			// do something useful with the response body
			// and ensure it is fully consumed
			EntityUtils.consume(entity2);
		} finally{
			response2.close();
		}
	}

	@Test
	public void r2() throws IOException{
		// The fluent API relieves the user from having to deal with manual deallocation of system resources
		// at the cost of having to buffer response content in memory in some cases.

		Content content = Request.Get("http://www.baidu.com")
				.execute().returnContent();
		System.out.println("content.asString(): " + content.asString(Charset.forName("UTF-8")));
		System.out.println("content type: " + content.getType());//text/html
		System.out.println("content type: " + content.getType().getCharset());//
		System.out.println(content.getType().getMimeType());//text/html
//		content.getType().getParameter("name");


	}

	@Test
	public void r3() throws IOException{
		Request.Post("http://targethost/login")
				.bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
				.execute().returnContent();
	}
}
